package com.simblog.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.simblog.common.constants.Constants;
import com.simblog.oaknt.domain.User;


public class CommonInterceptor extends HandlerInterceptorAdapter{
	private static final Logger logger = LoggerFactory.getLogger(CommonInterceptor.class);

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		
		//   http://127.0.0.1:8080/web/user/add.jhtml
		String uri = request.getRequestURI();  //  /web/user/add.jhtml
		String contextPath = request.getContextPath();   //   /web
		String url = uri.substring(contextPath.length());  //  /user/add.jhtml
		
		User user = (User) request.getSession().getAttribute(Constants.LOGIN_USER);
		
		if(user == null){
			logger.warn("========>用户未登录<========");
			//request.getRequestDispatcher("/p/login.jhtml").forward(request, response);
		    response.sendRedirect(contextPath + "/p/login.jhtml");
			return false;
		}
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		super.postHandle(request, response, handler, modelAndView);
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		super.afterCompletion(request, response, handler, ex);
	}
	
	
	
	
}
